package com.example.controller;

import com.example.common.AppResult;
import com.example.common.ResultCode;
import com.example.model.Board;
import com.example.service.IBoardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: bjc
 * Date: 2025-10-10
 * Time: 16:46
 */
@Slf4j
@Tag(name = "板块接口")
@RestController
@RequestMapping("/board")
public class BoardController {
    @Autowired
    private IBoardService boardService;

    //从配置文件中读取值，如果没有配置，则默认值为9，即显示9个板块
    @Value("${forum.index.board-num:9}")
    private Integer indexBoardNum;

    /**
     * 获取首页中的板块列表
     * @return
     */
    @Operation(summary = "获取首页中显示的板块")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList(){
        log.info("获取板块列表个数："+indexBoardNum);
        //1.调用service层方法
        List<Board> boards = boardService.selectByNum(indexBoardNum);
        //2.判断是否为空
        if(boards == null){
            boards = new ArrayList<>();
        }
        //3.返回结果
        return AppResult.success(boards);
    }
    /**
     * 根据板块id获取板块信息
     * @param boardId 板块id
     * @return 板块信息
     */
    @Operation(summary = "根据板块id获取板块信息")
    @GetMapping("/getBoardInfo")
    public AppResult<Board> getBoadInfo(
            @Parameter(description = "板块Id") @RequestParam("boardId") @NonNull Long boardId){
        //1.调用service层方法
        Board board = boardService.selectById(boardId);
        //2.对接口进行校验
        //板块记录在数据库中不存在，或者是已删除状态，则返回错误信息
        if(board == null || board.getDeleteState() == 1){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            //抛出异常
            return AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS);
        }
        //3.返回结果
        return AppResult.success(board);
    }
}
